Skip to content

feat(search): OpenSearch relevance with typos, prefixes, phrases and recency - #9864

Draft
salman1993 wants to merge 6 commits into
smohammed/opensearch-switch-shadowfrom
smohammed/opensearch-relevance
Draft

salman1993 wants to merge 6 commits into
smohammed/opensearch-switch-shadowfrom
smohammed/opensearch-relevance

Conversation

@salman1993

@salman1993 salman1993 commented Nov 21, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Stacked on #9863; #9838, #9862 and #9947 are merged. The earlier PRs fill an OpenSearch message index and add a relay-wide switch with a shadow mode. Until now the OpenSearch query was a plain word match, so shadow results could not be judged. This PR adds the relevance model: typos, partial words, rare words, phrases and recency. Search for users is unchanged while the flag stays at 0.

Mapping version 2 (buzz-messages-v2). Message text is indexed three ways: whole words (with accents folded and compound words split), stems, and prefixes of 2 to 10 characters. A partial word longer than 10 characters is cut to 10 at query time, so it still matches.

Query. It lives in crates/buzz-search/src/opensearch/relevance.rs:

  • Exact words score highest. A stem match counts 0.4 and a typo match 0.3. Typos keep the first letter fixed. In prefix mode the unfinished last word also matches with a typo once it has 4 characters; a shorter one is a single edit from too many other words.
  • Newest-first sort matches no typos and computes no scores. For a short word most typo matches are other words (log and lag), and with time as the only order they pushed exact matches out: on a million messages log matched 390 messages exactly and 1,623 with typos. Scoring made a newest-first search for a common word about six times slower (13 ms against 2 ms).
  • The query is split at _, . and case changes like the indexed text, so part of an identifier finds it (snake_case of snake_case_name, OutboxBatch of SearchOutboxBatch, BUZZ_OPEN of a longer variable name). foo_bar therefore also finds a message holding foo and bar apart.
  • A query of three or more words needs most of them, not all (2<75%).
  • The top 1,000 hits are rescored. A phrase or near-phrase match gets a bonus, and the score is scaled by age: 0.2 + 0.8 × 0.5^(age / 30 days). An old message keeps at least a fifth of its score.
  • Relevance results end at the rescore window. Paging past it would repeat hits, so the window is tied at compile time to the largest page the relay scans (1,000).

Upgrade. ensure() refuses to start indexing when the alias points at an index with another mapping version. Rows stay queued. The fix is in docs/opensearch-search-deployment.md: delete the old index, and the relay creates the new one, clears the backfill progress and rebuilds it. The same doc now covers edits, the refresh delay and lag after an outage.

Worth knowing:

  • The prefix field is the main cost. On 1 million synthetic messages the index was about 1.6 KB per message, and prefixes were about half of it. That is why the cap is 10 characters and not 20. OpenSearch's built-in index_prefixes came out larger than edge n-grams.
  • Bulk indexing on this mapping was about 3.8k documents per second on a laptop, roughly a quarter of the rate without the prefix field. Rebuild time is the number to watch.
  • A full rebuild of 1 million messages through the relay ran at about 1.3k, 2.1k, 2.9k and 3.0k documents per second at 1, 2, 4 and 8 workers. Scaling stops at 4 because the backfill and the drain take turns. Live messages were indexed with a median lag under 1 second during the rebuild.
  • The index is created with the default single shard.
  • The weights are first values. Shadow mode exists to tune them.

Related issue

N/A. Stacked on #9863; only the top commit is this PR.

Testing

On a laptop with local Postgres and OpenSearch 2.19.3 (512 MiB heap), 1 million synthetic messages:

  • Query p50 was 4, 6 and 10 ms for queries that match 10k, 100k and 790k messages.

  • A real relay with the flag at 1 ran prefix and typo searches; each wrote a shadow comparison line with OpenSearch hits.

  • A leftover version 1 index was refused and rows stayed queued. The documented rebuild brought the index up at version 2.

  • relevance_pages_tile_one_order_up_to_the_rescore_window checks that pages do not repeat or skip hits.

Not measured: Amazon OpenSearch Serverless, production-shaped data, and relevance quality against real queries.

🤖 Generated with Claude Code

@salman1993
salman1993 force-pushed the smohammed/opensearch-switch-shadow branch from 4c7562a to de3cc36 Compare November 24, 2026 21:14
@salman1993
salman1993 force-pushed the smohammed/opensearch-relevance branch from 85bc1c6 to 253687d Compare November 24, 2026 21:22
@salman1993
salman1993 force-pushed the smohammed/opensearch-switch-shadow branch from de3cc36 to 1942416 Compare November 25, 2026 00:25
@salman1993
salman1993 force-pushed the smohammed/opensearch-relevance branch from 253687d to b0aef97 Compare November 25, 2026 00:34
@salman1993
salman1993 force-pushed the smohammed/opensearch-switch-shadow branch from 1942416 to 6a93e8c Compare November 25, 2026 18:49
@salman1993
salman1993 force-pushed the smohammed/opensearch-relevance branch from b0aef97 to 367d560 Compare November 25, 2026 19:27
@salman1993
salman1993 force-pushed the smohammed/opensearch-switch-shadow branch from 6a93e8c to 107f35e Compare November 25, 2026 19:58
@salman1993
salman1993 force-pushed the smohammed/opensearch-relevance branch from 367d560 to 2bf6f7d Compare November 25, 2026 20:08
@salman1993
salman1993 force-pushed the smohammed/opensearch-switch-shadow branch 2 times, most recently from 117aef8 to f406300 Compare November 25, 2026 20:50
@salman1993
salman1993 force-pushed the smohammed/opensearch-relevance branch from 2bf6f7d to b5ddd4d Compare November 25, 2026 20:50
@salman1993
salman1993 force-pushed the smohammed/opensearch-switch-shadow branch from f406300 to ba75249 Compare November 26, 2026 00:23
@salman1993
salman1993 force-pushed the smohammed/opensearch-relevance branch from b5ddd4d to 1bac232 Compare November 26, 2026 16:19
salman1993 and others added 4 commits November 26, 2026 12:46
…recency

Message search on OpenSearch now matches words one or two typos away and
word prefixes, keeps letters and digits of a token together, and ranks
exact phrases and nearby words higher. Newer messages rank higher through
an age rescore.

Phrase, proximity and age are applied as rescores over the top 1,000
hits, not to every match, so a common word in a large community stays
cheap. Relevance results end at that window, which is tied at compile
time to the largest page the relay serves.

The mapping version moves to 2, so the index is recreated and backfilled.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Signed-off-by: Salman Mohammed <smohammed@squareup.com>
The analyzers and the decay rescore pass on a NextGen Serverless
collection. Serverless reports every shard count as zero, so only
timed_out can show a partly failed search there.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Signed-off-by: Salman Mohammed <smohammed@squareup.com>
The relay clears the backfill progress itself when it creates the index.

Signed-off-by: Salman Mohammed <smohammed@squareup.com>
Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
…hen sorting by time

The query analyzers did not split words the way the index analyzers do, so
part of an identifier (snake_case of snake_case_name, OutboxBatch of
SearchOutboxBatch) found nothing, and neither did the start of an
identifier longer than 24 characters in prefix mode.

Sorted by time, a search matched typos too. Weights do not order those
results, so for a short word the other words one letter away pushed the
exact matches out. It also asked for scores nothing reads, which made a
common word's search about six times slower on a million messages.

Signed-off-by: Salman Mohammed <smohammed@squareup.com>
Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
@salman1993
salman1993 force-pushed the smohammed/opensearch-relevance branch from 39227f9 to 8b7d435 Compare November 26, 2026 17:57
salman1993 and others added 2 commits November 26, 2026 19:38
…, then apart

A query's identifier is split like indexed text, which makes its parts a phrase on the exact field. The stem and prefix fields keep no positions, so parts apart still match there, below. Cover both for snake and camel case, alone, in a longer query and unfinished.

Signed-off-by: Salman Mohammed <smohammed@squareup.com>
Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
- In prefix mode the unfinished last word now also matches with a typo,
  from four characters, so a single misspelt word finds its message
  below the exact spelling. Shorter words are one edit from too many
  others. Newest order still matches no typos.
- Score parts report an age factor of 1 for a search sorted by time,
  which applies none, and the doc no longer names a stale window size.
- Drop the search analyzers that equalled their index analyzers.
- The age factor uses abs_diff, so extreme timestamps cannot overflow,
  and the half-life is a whole number of days.
- The deployment doc says why prefix searches overlap less in shadow.

Signed-off-by: Salman Mohammed <smohammed@squareup.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant